#!/usr/bin/python
#coding=utf-8

#author   : mayongfan
#version  : 1.0
#datetime : 2023.12.11

import os
import time
import requests
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.styles import Font, Fill
from openpyxl.styles.colors import RED
import pandas as pd
import numpy as np

fontRed = Font(color=RED, bold=True)
fontBold = Font(bold=True)

redBalls = []
blueBalls = []
omitTable = [[-1 for col in range(34)] for row in range(2000)]
omitTable_blue = [[-1 for col in range(17)] for row in range(2000)]

arrSeral = []
arrDate = []
startSeral = 0

"获取网络数据"
"sNo: 最近期数"
"sNo=0: 为输入截止日期方法查询"
def getData(sNo):
    global arrSeral, arrDate
    headers ={
        'User-Agent': 'Mozilla/5.0(X11;Linux aarch64)AppleWebKit/537.36(KHTML, likeGecko) Chrome / 77.0.3770.100Safari / 537.36'
    }
    url = 'https://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq'
    url += '&issueCount=' + str(sNo) + '&issueStart=&issueEnd=&dayStart=&dayEnd=&pageNo=1&pageSize='+str(sNo)+'&week=&systemType=PC'
    response = requests.get(url,headers=headers)
    # print(response)
    json_data = response.json()
    for r in json_data['result']:
        arrSeral.append(r['code'])
        arrDate.append(r['date'])
        integers_red = [int(item) for item in r['red'].split(',')]
        integers_blue = int(r['blue'])
        redBalls.append(integers_red)
        blueBalls.append(integers_blue)
    setOmitTable()
    setOmitTable_blue()
def setOmitTable():
    for i in range(1, 34): # 1-33号码
        omit = 0
        for j in range(99, -1, -1):
            for k in range(0, 6):
                if redBalls[j][k] == i:
                    omit = 0
            omitTable[j][i] = omit
            omit = omit + 1
def setOmitTable_blue():
    for i in range(1, 17): # 1-33号码
        omit = 0
        for j in range(99, -1, -1):
            if blueBalls[j] == i:
                omit = 0
            omitTable_blue[j][i] = omit
            omit = omit + 1
def applyXlsFont(ws):
    rowNum = ws.max_row
    ws['A%d' % rowNum].font = fontBold
    ws['B%d' % rowNum].font = fontBold
    ws['C%d' % rowNum].font = fontBold
    ws['D%d' % rowNum].font = fontBold
    ws['E%d' % rowNum].font = fontBold
    ws['F%d' % rowNum].font = fontBold
    ws['G%d' % rowNum].font = fontBold
    ws['H%d' % rowNum].font = fontBold
    ws['I%d' % rowNum].font = fontBold
    ws['J%d' % rowNum].font = fontBold
    ws['K%d' % rowNum].font = fontBold
    ws['L%d' % rowNum].font = fontRed
    ws['M%d' % rowNum].font = fontBold
    ws['N%d' % rowNum].font = fontBold
    ws['O%d' % rowNum].font = fontBold
    ws['P%d' % rowNum].font = fontBold
    ws['Q%d' % rowNum].font = fontBold
    ws['R%d' % rowNum].font = fontBold
    ws['S%d' % rowNum].font = fontBold
    ws['T%d' % rowNum].font = fontBold
    ws['U%d' % rowNum].font = fontBold
def combine(arr, k):
    # 结果列表
    result = []

    # 定义递归函数
    def recurse(start, end, curr):
        # 如果当前组合的长度等于k，添加到结果中
        if len(curr) == k:
            result.append(curr[:])
            return
        # 递归遍历，生成新的组合
        for i in range(start, end + 1):
            curr.append(arr[i])
            recurse(i + 1, end, curr)
            curr.pop()

    # 调用递归函数
    recurse(0, len(arr) - 1, [])
    return result
def has_consecutive_numbers(permutation):
    """
    检查排列中是否存在超过两个连续数字
    """
    for i in range(len(permutation) - 2):
        if permutation[i] + 1 == permutation[i+1] and permutation[i+1] + 1 == permutation[i+2]:
            return True
    return False
def calculate_ac(numbers):
    """
    计算乐透型彩票号码的AC值（数字复杂指数）。
    参数：
    numbers (list): 彩票号码列表，假设列表中的元素都是唯一的。
    返回：
    int: AC值。
    """
    # 计算差值的绝对值，并去重
    differences = set(abs(i - j) for i in numbers for j in numbers if i != j)
    # 计算AC值
    ac_value = len(differences) - (len(numbers) - 1)
    return ac_value

if __name__ == '__main__':
    startTime = time.time()
    # getData(2000)
    # for redBall in redBalls:
    #    print(calculate_ac(redBall))
    nums = [8,1,2,17,21,22,23,5,24,7,25,9,27,30,31,32]
    combines = combine(nums,6)
    combines_yes = []
    for comb in combines:
        if not has_consecutive_numbers(comb) and calculate_ac(comb)==7:
            print(comb)
            combines_yes.append(comb)
    print(len(combines_yes))
    endTime = time.time()
    print("总共运行{}s".format(endTime-startTime))
